<?php
namespace app\admin\model;

use think\Db;
use think\facade\Config;
use think\Model;

class TestPlatConfig extends Model
{
    protected $table = 'bus_test_plat_config';
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'create_time';

    public static function add($request)
    {
        $configInfo = self::where('cid',$request['cid'])->where('plat_id',$request['plat_id'])->find();
        if ($configInfo) {
            return ['code' => 422, 'msg' => '请勿重复添加'];
        } else {
            if (self::create($request)) {
                deleteCache();
                return ['code' => 200, 'msg' => '添加成功'];
            } else {
                return ['code' => 422, 'msg' => '添加失败'];
            }
        }
    }

    public static function upd($request)
    {
        $configInfo = self::where('cid',$request['cid'])->where('plat_id',$request['plat_id'])->where('id', 'neq', $request['id'])->find();
        if ($configInfo) {
            return ['code' => 422, 'msg' => '请勿重复添加'];
        } else {
            if (self::update($request, ['id' => $request['id']])) {
                deleteCache();
                return ['code' => 200, 'msg' => '更新成功'];
            } else {
                return ['code' => 422, 'msg' => '更新失败'];
            }
        }
    }

    public static function getConfig($request)
    {
        $where = [];


        if($request['cid'] != ''){
            $where[] = ['pc.cid', '=', $request['cid']];
        }


        if($request['platId'] != ''){
            $where[] = ['pc.plat_id', '=', $request['platId']];
        }

         return self::alias('pc')
                ->field('pc.*,bc.company_name,bp.platfrom,bp.plat_type,cb.company_name as check_channel_name,cb.channel_id as check_channel_id')
                ->join('bus_channel bc','bc.channel_id = pc.cid','LEFT')
                ->join('bus_channel cb','cb.channel_id = pc.check_cid','LEFT')
                ->join('bus_platfrom bp','bp.plat_id = pc.plat_id','LEFT')
                ->where($where)
                ->paginate($request['limit'], false, $request)
                ->each(function($item, $key){
                    $item->company_name = $item->cid . '/' . $item->company_name;
                    $item->plat_ = $item->plat_id . '/' . $item->platfrom;
                    switch ($item->status) {
                        case '0':
                            $item->status = '<span class="layui-badge status-jy">禁用</span>';
                            break;
                        case '1':
                            $item->status = '<span class="layui-badge layui-bg-green status-qy">正常</span>';
                            break;
                        default:
                            $item->status = '未知';
                            break;
                    }
                    if ($item->plat_type == 'CMPP') {
                        $item->check_channel = $item->check_channel_id . '/' . $item->check_channel_name;
                    } else {
                        $item->check_channel = '';
                    }
                })
                ->toArray();
    }

}
